﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using Kugar.Core.ExtMethod;
using Kugar.UI.Web.AdminLTE.Helpers;
using Microsoft.AspNetCore.Mvc.Rendering;

namespace Kugar.UI.Web.AdminLTE.PageControls.PageForms
{
    public class PageForm_RadioListField: PageForm_SingleInputFieldBase
    {
        private string _checkedValue = null;
        private SelectorItem[] _options = null;
        private SelectorItem _firstItem = null;
        private bool _disabled = false;
        //private string _name = "";

        public PageForm_RadioListField(PageForm_Form form, ViewContext viewContext) : base(form, viewContext)
        {
            this.Css("webui-form_radiolistfield");
        }

        //public PageForm_RadioListField Name(string name)
        //{
        //    _name = name;

        //    return this;
        //}

        public PageForm_RadioListField Checked(string checkedValue)
        {
            _checkedValue = checkedValue;

            return this;
        }

        public PageForm_RadioListField Checked(int checkedValue)
        {
            _checkedValue = checkedValue.ToString();

            return this;
        }

        public PageForm_RadioListField Options(IEnumerable<SelectorItem> options)
        {
            _options = options.ToArrayEx();

            return this;
        }

        public PageForm_RadioListField Options(params (string title, string value)[] options)
        {
            _options = options.Select(x => new SelectorItem(x.title, x.value)).ToArrayEx();

            return this;
        }

        public PageForm_RadioListField Options<TData>(IEnumerable<TData> values, Func<TData, string> titleGetter,
            Func<TData, string> valueGetter, Func<TData, bool> disabledStateGetter = null)
        {
            _options = values.Select(x => new SelectorItem(titleGetter(x), valueGetter(x), disabledStateGetter?.Invoke(x)??false))
                .ToArrayEx();

            return this;
        }

        /// <summary>
        /// 选项列表,根据枚举类型
        /// </summary>
        /// <param name="enumType"></param>
        /// <returns></returns>
        public PageForm_RadioListField Options(Type enumType, Func<SelectorItem, bool> disabledGetter = null)
        {
            if (!enumType.IsEnum)
            {
                throw new ArrayTypeMismatchException("enumType参数类型必须为枚举类型");
            }

            //var names = enumType.GetEnumNames();
            //var values = (int[])enumType.GetEnumValues();

            //var lst = new List<SelectorItem>(names.Length);

            //for (int i = 0; i < names.Length; i++)
            //{
            //    FieldInfo enumInfo = enumType.GetField(names[i]);

            //    var a = (DescriptionAttribute)enumInfo.GetCustomAttributes(typeof(DescriptionAttribute), false).FirstOrDefault();

            //    var k = new SelectorItem(a != null ? a.Description : names[i].ToStringEx(), values[i].ToString());

            //    if (disabledGetter!=null)
            //    {
            //        k.Disabled = disabledGetter(k);
            //    }

            //    lst.Add(k);
            //}

            //_options = lst.ToArrayEx();

            _options = Utility.GetEnumTypeToSelectorItems(enumType);

            if (disabledGetter != null)
            {
                foreach (var opt in _options)
                {
                    opt.Disabled = disabledGetter(opt);
                }
            }

            return this;
        }

        public PageForm_RadioListField Disabled(bool disabled = true)
        {
            _disabled = disabled;

            return this;
        }

        public PageForm_RadioListField FirstItem(SelectorItem item)
        {
            _firstItem = item;

            return this;
        }

        public PageForm_RadioListField FirstItem(string title, string value)
        {
            _firstItem = new SelectorItem(title, value);

            return this;
        }

        protected override void RenderContent(TextWriter writer)
        {
            if (string.IsNullOrWhiteSpace(_checkedValue))
            {
                if (_firstItem!=null)
                {
                    _checkedValue = _firstItem.Value;
                }
                else
                {
                    _checkedValue = _options.FirstOrDefault()?.Value;
                }
            }

            if (_firstItem!=null)
            {
                //var isCheck = !_options.Any(x => x.Value == _checkedValue);

                writer.WriteLine($"<input type=\"radio\" {(_checkedValue == _firstItem.Value).If("checked")} name=\"{_name}\" title=\"{_firstItem.Title}\" value=\"{_firstItem.Value}\" {(_disabled).If("disabled")} lay-skin=\"primary\">");
            }

            foreach (var option in _options)
            {
                writer.WriteLine($"<input type=\"radio\" {(_checkedValue == option.Value).If("checked")} name=\"{_name}\" title=\"{option.Title}\" value=\"{option.Value}\" {(_disabled || option.Disabled).If("disabled")} lay-skin=\"primary\">");
            }
        }
    }
}
